package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.shopping.ShoppingCartService;
import com.mall.user.UserService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/shopping")
@Anoymous
public class OrderController {

    @Reference(timeout = 3000,check = false)
    OrderCoreService orderCoreService;

    @Reference(timeout = 3000,check = false)
    OrderQueryService orderQueryService;

    @Reference(timeout = 3000,check = false)
    UserService userService;

    @Reference(timeout = 3000,check = false)
    ShoppingCartService shoppingCartService;

    //增加订单
    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest){
        Cookie[] cookies = servletRequest.getCookies();
        String token = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(TokenIntercepter.ACCESS_TOKEN)){
                token = cookie.getValue();
                break;
            }
        }
        String uid = userService.getUid(token);
        Long userId = Long.parseLong(uid);
        request.setUserId(userId);
        request.setUniqueKey(UUID.randomUUID().toString());

        //获得订单ID
        CreateOrderResponse response = orderCoreService.createOrder(request);

//        //查询该用户的该订单的所有商品id
//        List<Long> itemId = orderQueryService.checkItemByOrderId(response.getOrderId());
//
//        //根据上面获得的itemId,去shopping服务中删除购物车
//        shoppingCartService.deleteSomeCart(request.getUserId(),itemId);

        System.out.println(response);
        System.out.println();
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    //查看订单
    @GetMapping("order")
    public ResponseData checkOrder(OrderListRequest request, HttpServletRequest servletRequest){
        Cookie[] cookies = servletRequest.getCookies();
        String token = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(TokenIntercepter.ACCESS_TOKEN)){
                token = cookie.getValue();
                break;
            }
        }
        String uid = userService.getUid(token);
        Long userid = Long.parseLong(uid);
        request.setUserId(userid);
        //request.setUniqueKey(UUID.randomUUID().toString());

        //request.setUserId((long)71);

        OrderListResponse response = orderQueryService.checkOrder(request);
        //System.out.println(response);
        System.out.println();
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }


    //查看订单细节
    @GetMapping("order/{orderId}")
    public ResponseData checkDetailOrder(@PathVariable("orderId") String orderId){

        OrderDetailResponseJAL response = orderQueryService.checkDetailOrder(orderId);
        System.out.println(response);
        System.out.println();
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    //取消订单
    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest , HttpServletRequest servletRequest){
        //String userInfo = (String)servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //JSONObject object = JSON.parseObject(userInfo);
        //Long userid = Long.parseLong(object.get("uid").toString());
        //request.setUserId(userid);

        CancelOrderResponse response = orderCoreService.cancelOrder(cancelOrderRequest);
        System.out.println(response);
        System.out.println();
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    //删除订单
    @DeleteMapping("order/{orderId}")
    public ResponseData deletelOrder(@PathVariable("orderId") String orderId , HttpServletRequest servletRequest){
        //String userInfo = (String)servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //JSONObject object = JSON.parseObject(userInfo);
        //Long userid = Long.parseLong(object.get("uid").toString());
        //request.setUserId(userid);

        DeleteOrderResponse response = orderCoreService.deleteOrder(orderId);
        System.out.println(response);
        System.out.println();
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
